home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / latex209 / contrib / textyl / psrc / tylext.c < prev    next >
C/C++ Source or Header  |  1993-11-07  |  4KB  |  139 lines

  1. /* External procedures for dvitype                */
  2. /*   Written by: H. Trickey, 2/19/83 (adapted from TeX's ext.c) */
  3.  
  4. #include "texpaths.h"    /* defines default TEXFONTS path */
  5. #include "h00vars.h"        /* defines Pascal I/O structure */
  6.  
  7. char *fontpath;
  8.  
  9. char *getenv();
  10.  
  11. /*
  12.  * setpaths is called to set up the pointer fontpath
  13.  * as follows:  if the user's environment has a value for TEXFONTS
  14.  * then use it;  otherwise, use defaultfontpath.
  15.  */
  16. setpaths()
  17. {
  18.     register char *envpath;
  19.     
  20.     if ((envpath = getenv("TEXFONTS")) != NULL)
  21.         fontpath = envpath;
  22.     else
  23.         fontpath = defaultfontpath;
  24. }
  25.  
  26. #define namelength 100   /* should agree with defn in textyl program*/
  27. extern char thefilename[],realnameoffile[]; /* these have size namelength */
  28.  
  29. /*
  30.  *    testaccess(amode,filepath)
  31.  *
  32.  *  Test whether or not the file whose name is in the global thefilename
  33.  *  can be opened for reading (if mode=READACCESS)
  34.  *  or writing (if mode=WRITEACCESS).
  35.  *
  36.  *  The filepath argument is one of the ...FILEPATH constants defined below.
  37.  *  If the filename given in thefilename does not begin with '/', we try 
  38.  *  prepending all the ':'-separated areanames in the appropriate path to the
  39.  *  filename until access can be made, if it ever can.
  40.  *
  41.  *  The realnameoffile global array will contain the name that yielded an
  42.  *  access success.
  43.  */
  44.  
  45. #define READACCESS 4
  46. #define WRITEACCESS 2
  47.  
  48. #define NOFILEPATH 0
  49. #define FONTFILEPATH 3
  50.  
  51. bool
  52. testaccess(amode,filepath)
  53.     int amode,filepath;
  54. {
  55.     register    bool ok;
  56.     register char  *p;
  57.     char   *curpathplace;
  58.     int     f;
  59.  
  60.     switch (filepath) {
  61.     case NOFILEPATH: 
  62.         curpathplace = NULL;
  63.         break;
  64.     case FONTFILEPATH: 
  65.         curpathplace = fontpath;
  66.         break;
  67.     }
  68.     if (thefilename[0] == '/')    /* file name has absolute path */
  69.     curpathplace = NULL;
  70.     do {
  71.     packrealnameoffile (&curpathplace);
  72.     if (amode == READACCESS)/* use system call "access" to see if we
  73.                    could read it */
  74.         if (access (realnameoffile, READACCESS) == 0)
  75.         ok = TRUE;
  76.         else
  77.         ok = FALSE;
  78.     else {
  79.     /* WRITEACCESS: use creat to see if we could create it, but close
  80.        the file again if we''re OK, to let pc open it for real */
  81.         f = creat (realnameoffile, 0666);
  82.         if (f >= 0)
  83.         ok = TRUE;
  84.         else
  85.         ok = FALSE;
  86.         if (ok)
  87.         close (f);
  88.     }
  89.     } while (!ok && curpathplace != NULL);
  90.     if (ok) {            /* pad realnameoffile with blanks, as
  91.                    Pascal wants */
  92.     for (p = realnameoffile; *p != '\0'; p++)
  93.                 /* nothing: find end of string */
  94.         ;
  95.     while (p < &(realnameoffile[namelength]))
  96.         *p++ = ' ';
  97.     }
  98.     return (ok);
  99. }
  100.  
  101. /*
  102.  * packrealnameoffile(cpp) makes realnameoffile contain the directory at *cpp,
  103.  * followed by '/', followed by the characters in thefilename up until the
  104.  * first blank there, and finally a '\0'.  The cpp pointer is left pointing
  105.  * at the next directory in the path.
  106.  * But: if *cpp == NULL, then we are supposed to use thefilename as is.
  107.  */
  108. packrealnameoffile(cpp)
  109.     char **cpp;
  110. {
  111.     register char  *p,
  112.                    *realname;
  113.  
  114.     realname = realnameoffile;
  115.     if ((p = *cpp) != NULL) {
  116.     while ((*p != ':') && (*p != '\0')) {
  117.         *realname++ = *p++;
  118.         if (realname == &(realnameoffile[namelength - 1]))
  119.         break;
  120.     }
  121.     if (*p == '\0')
  122.         *cpp = NULL;    /* at end of path now */
  123.     else
  124.         *cpp = p + 1;    /* else get past ':' */
  125.     *realname++ = '/';    /* separate the area from the name to
  126.                    follow */
  127.     }
  128.  /* now append thefilename to realname... */
  129.     p = thefilename;
  130.     while (*p != ' ') {
  131.     if (realname >= &(realnameoffile[namelength - 1])) {
  132.         fprintf (stderr, "! Full file name is too long\n");
  133.         break;
  134.     }
  135.     *realname++ = *p++;
  136.     }
  137.     *realname = '\0';
  138. }
  139.